
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private static final String jdbcUrl = "jdbc:mysql://192.168.10.129:3306/kq";
    private static final String user = "root";
    private static final String password = "root";

    // 可以在静态初始化块中加载驱动程序，但这通常是可选的
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // MySQL Connector/J 8.0+
            // 或者对于旧版本的MySQL Connector/J
            // Class.forName("com.mysql.jdbc.Driver"); // MySQL Connector/J 5.1.x
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("MySQL JDBC Driver not found.", e);
        }
    }

    public List<User> query() {
        List<User> userList = new ArrayList<>();

        try (
                Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
                PreparedStatement stmt = conn.prepareStatement("SELECT class,sumwoman,summan FROM stu_4_filtered LIMIT 6")
        ) {
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                // 假设 class 列是整数类型，sumwoman 是字符串类型，summan 也是整数类型
                int className = rs.getInt("class");
                String count1 = rs.getString("sumwoman");
                int count2 = rs.getInt("summan");

                // 确保User类的构造函数接受正确的参数类型
                User user = new User(className, count1, count2);
                userList.add(user);
                System.out.println(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return userList;
    }
}